package com.wuzuqing.android.eightdiagrams.task;

import com.wuzuqing.android.eightdiagrams.model.QiMenDunJiaItem;
import com.wuzuqing.android.eightdiagrams.model.QiMenDunJiaModel;

import java.util.ArrayList;
import java.util.List;

/**
 * @CopyRight: 广州仰望星空云科技有限公司
 * @Author : wuzuqing
 * @Email:wuzuqing@xinyu668.com
 * @CreateTime: 26.7.24 3:30 PM
 * @Description: 请写描述
 */
public class QiMenDunJiaStep5Task extends IQiMenDunJiaTask {
    QiMenDunJiaModel model;

    public QiMenDunJiaStep5Task(QiMenDunJiaModel model) {
        this.model = model;
    }

    @Override
    public void handleTask() {
        List<QiMenDunJiaItem> list = model.getList();
        //定天盘三奇六仪
        int zhiGongNum = model.getOriginGongNum();
        String zhiGongGan = model.getLunar().getBaZi().get(3).charAt(0) + "";
        int[] gongArr = new int[9];
        boolean isYangDun = model.isYangDun();
        if (model.isShiGanJia()) {
            zhiGongGan = model.getZhiGan();
        }
        if (zhiGongNum == 5) {
            zhiGongNum = 2;
        }
        int zhiGongIndex = listArr.indexOf(zhiGongNum);
        for (int i = 0; i < list.size(); i++) {
            QiMenDunJiaItem dunJia = list.get(i);
            if ((dunJia.getDiPanGan().startsWith(zhiGongGan))) {
                int num = dunJia.getGongNum();
                if (num == 5) {
                    num = 2;
                }
                int index = listArr.indexOf(num);
                for (int k = 0; k < listArr.size(); k++) {
                    int realIndex = (index + k) % listArr.size();
                    int realGongNum = listArr.get(realIndex);
                    QiMenDunJiaItem temp = getDunJia(list, realGongNum);

                    if (temp != null) {
                        //定九星
                        if (!model.isShiGanJia()) {
                            //天盘三奇六仪
                            try {
                                temp.setTianPanGan(getDiPanGan(list,
                                        listArr.get((zhiGongIndex + k) % listArr.size())));
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    }
                }
                String[] shenArr = new String[]{
                        "值符",
                        "腾蛇",
                        "太阴",
                        "六合",
                        "白虎",
                        "玄武",
                        "九地",
                        "九天"
                };
                try {
                    index = isYangDun ? listArr.indexOf(num) :
                            yinListArr.indexOf(num);
                    for (int j = 0; j < listArr.size(); j++) {
                        if (isYangDun) {
                            gongArr[j] = listArr.get((index + j) % listArr.size());
                        } else {
                            gongArr[j] = yinListArr.get((index + j) % yinListArr.size());
                        }
                        QiMenDunJiaItem temp = getDunJia(list, gongArr[j]);
                        if (temp != null) {
                            //定八神
                            temp.setShen(shenArr[j]);
                        }
                    }
                } catch (Exception e) {
                    System.err.println(e);
//                    System.err.println("getGongNum=" + dunJia.getGongNum());
                }
            }
        }
    }
}
